package xiaoka.ecarx.com.cn.youlong.upnp;

import android.content.Intent;
import android.util.Log;

import org.teleal.cling.binding.annotations.UpnpAction;
import org.teleal.cling.binding.annotations.UpnpInputArgument;
import org.teleal.cling.binding.annotations.UpnpOutputArgument;
import org.teleal.cling.binding.annotations.UpnpService;
import org.teleal.cling.binding.annotations.UpnpServiceId;
import org.teleal.cling.binding.annotations.UpnpServiceType;
import org.teleal.cling.binding.annotations.UpnpStateVariable;

/**
 * Created by Chong Zhao on 2016/10/30.
 */
@UpnpService(
        serviceId = @UpnpServiceId("MessageHandler"),
        serviceType = @UpnpServiceType(value = "MessageHandler", version = 1)
)
public class MessageHandler {
    private static final String TAG = "MessageHandler";
    @UpnpStateVariable(defaultValue = "")
    private String msg = "";
    
    @UpnpStateVariable(defaultValue = "")
    private String from = "";

    @UpnpAction(out = @UpnpOutputArgument(name = "Ret"))
    public String setMsg(@UpnpInputArgument(name = "Msg") String newMsg,
                         @UpnpInputArgument(name = "From") String from) {
    	this.msg = newMsg;
        this.from = from;
        System.out.println("receive new msg:" + newMsg);
        //对异常信息进行处理
        if (newMsg == null&&from==null) {
            return "error";
        }
         //进行消息处理
        messageReceivedPlayer(newMsg,from);
        return "Received";
    }
    //获取信息进行发送广播
    public void messageReceivedPlayer(String msg, String from) {
        MainActivity.mInstance.sendBroadcast(new Intent(msg));
        Log.d(TAG, "messageReceived: "+msg);
    }

}
